package com.gao.util;

import javax.servlet.*;
import javax.servlet.annotation.HandlesTypes;
import javax.servlet.http.HttpServlet;
import java.util.Set;

/**
 * @author gao
 * @time 2020/12/22 23:40:14
 */

@HandlesTypes(HttpServlet.class)
public class MyServletContainerInitializer implements ServletContainerInitializer {
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        System.out.println("MyServletContainerInitializer.onStartup");
        for (Class<?> clazz : set) {
            ServletRegistration.Dynamic servlet = servletContext.addServlet(clazz.getName(), (Class<? extends Servlet>) clazz);
            String name = clazz.getSimpleName();
            name = name.substring(0, name.indexOf("Servlet")).toLowerCase();
            System.out.println("name = " + name);
            servlet.addMapping("/" + name);
        }
    }
}
